/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.managedbean;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import mrp.productionPlanning.entity.MonthlyDetailEntity;
import mrp.productionPlanning.session.DailyDetailSessionBean;
import mrp.productionPlanning.session.MonthlyDetailSessionBean;
import mrp.productionPlanning.session.WeeklyDetailSessionBean;

/**
 *
 * @author Wei Haoyang
 */
@ManagedBean
@RequestScoped
public class GenerateCalendarManagedBean {
    
    
    private int year;
    @EJB
    MonthlyDetailSessionBean mdsb = new MonthlyDetailSessionBean();
    @EJB
    WeeklyDetailSessionBean wdsb = new WeeklyDetailSessionBean();
    @EJB
    DailyDetailSessionBean ddsb = new DailyDetailSessionBean();
    
    /** Creates a new instance of GenerateCalendarManagedBean */
    public GenerateCalendarManagedBean() {
    }
    
    public void generate(ActionEvent event) throws ParseException, IOException{
        Date temp = new Date();
        Calendar cal = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
        temp = sdf.parse("01/01/2000");
        cal.setTime(temp);
        cal.set(Calendar.YEAR, year);
        if (cal.get(Calendar.DAY_OF_WEEK) == 1 || cal.get(Calendar.DAY_OF_WEEK) == 7){
            cal.add(Calendar.DATE, 1);
        }
        if (ddsb.getDailyDetailByDate(temp) == null){
            ddsb.generateAYear(year);
            System.out.println("-------------------------------------Check Point3---------------------------------------");
            mdsb.generateAYear(year);
            System.out.println("-------------------------------------Check Point4---------------------------------------");
            wdsb.generateAYear(year);
            List<MonthlyDetailEntity> mds = new ArrayList<MonthlyDetailEntity>();
            for(MonthlyDetailEntity md:mds){
                mdsb.setDays(md);
            }
            FacesContext.getCurrentInstance().getExternalContext().redirect("test.xhtml");      
        }
        else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Indicated year has already been generated!", ""));
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Done Successfully.", ""));
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
}
